package com.crape.common.utils;

import java.util.ArrayList;
import java.util.List;

public class PageUtils {

    public static Integer getPagedNum(Long counts, Integer pageSize) {
        if (counts == null || counts == 0 || pageSize == null || pageSize == 0) {
            return 0;
        }
        if (counts <= pageSize) {
            return 1;
        }
        if (counts % pageSize > 0) {
            return (int) ((counts / pageSize) + 1);
        }
        return (int) (counts / pageSize);
    }

    public static Integer[] getPagedNumArray(Long counts, Integer pageSize) {
        Integer pageNum = getPagedNum(counts, pageSize);
        if (pageNum == null || pageNum == 0) {
            return null;
        }
        List<Integer> pageNumArray = new ArrayList<Integer>();
        for (int i = 1; i <= pageNum; i++) {
            pageNumArray.add(i);
        }
        return pageNumArray.toArray(new Integer[pageNum]);
    }

    public static Integer getStartIndex(Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageNo == 0 || pageSize == null || pageSize == 0) {
            return 0;
        }
        return (pageNo - 1) * pageSize;
    }

}
